//
//  WKDownloadingCell.m
//  WeKeTang
//
//  Created by ichina on 2017/9/21.
//  Copyright © 2017年 Dezhi Tiancheng Education Technology (Beijing) Co. Ltd. All rights reserved.
//

#import "WKDownloadingCell.h"
#import <UIImageView+WebCache.h>
#import "WKFMDBTools.h"
@interface WKDownloadingCell ()

@property (nonatomic, strong) UIImageView *iconImageView;

@property (nonatomic, strong) YYLabel *titleLabel;

@property (strong, nonatomic)  UIProgressView *prograssView;

@property (strong, nonatomic) YYLabel *prograssLabel;

@property (nonatomic, strong) UIButton *btnSuspended;

@end

@implementation WKDownloadingCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI{
    
    self.iconImageView = [[UIImageView alloc]init];
    self.btnSuspended.alpha = 0.5f;
    [self.contentView addSubview:self.iconImageView];
    
    self.titleLabel = [[YYLabel alloc]init];
    [self.contentView addSubview:self.titleLabel];
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.textColor = [UIColor blackColor];
    
    self.prograssView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    self.prograssView.progressTintColor = [UIColor blueColor];
    self.prograssView.trackTintColor = [UIColor lightGrayColor];
    self.prograssView.layer.masksToBounds = YES;
    self.prograssView.layer.cornerRadius = 1;
    
    [self.contentView addSubview:self.prograssView];
    
    self.prograssLabel = [[YYLabel alloc]init];
    self.prograssLabel.font = [UIFont systemFontOfSize:13];
    self.prograssLabel.textColor = [UIColor colorWithHexString:@"#FDA60A"];
//    self.prograssLabel.text = @"300K/20M";
    [self.contentView addSubview:self.prograssLabel];
    
    self.btnSuspended = [UIButton buttonWithType:UIButtonTypeCustom];
    
    NSArray *dbDownLoadList = [[WKFMDBTools defaultFMDBToos] QueryDownloadingDataWithTableName:kDBTableName status:kStatusDowning].mutableCopy;
    NSLog(@"未完成：%@",dbDownLoadList);
    if (dbDownLoadList.count == 0) {//没有正在下载的视频
        [self.btnSuspended setImage:[UIImage imageNamed:@"icon_supendtip"] forState:UIControlStateNormal];
    }else{
         //说明有下载未完成的数据
        [self.btnSuspended setImage:[UIImage imageNamed:@"icon_downloading"] forState:UIControlStateNormal];
    }
    [self.btnSuspended addTarget:self action:@selector(btnSuspendedAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:self.btnSuspended];
    
}

- (void)setModel:(WKDownLoadModel *)model{
    _model = model;
    [self.iconImageView setImageWithURL:[NSURL URLWithString:model.section_imgurl] options:YYWebImageOptionShowNetworkActivity];
    self.titleLabel.text = model.section_name;
    NSInteger completeCount = [model.completeCount integerValue];
    NSInteger totalCount = [model.totalCount integerValue];
    
    CGFloat prograss = (CGFloat)completeCount/totalCount;
    
    [self.prograssView setProgress:prograss animated:YES];

    CGFloat currentLenth = (completeCount *485);
    CGFloat totlalLenth = (totalCount *485)/1024;
    if (currentLenth/1024 < 1) {
        self.prograssLabel.text = [NSString stringWithFormat:@"%.0fK/%.0fM",currentLenth,totlalLenth];
    }else if (currentLenth/1024 > 1){
        currentLenth = currentLenth/1024;
        self.prograssLabel.text = [NSString stringWithFormat:@"%.2fM/%.0fM",currentLenth,totlalLenth];
    }
    NSLog(@"下载个数：%@-------%@",model.completeCount,model.totalCount);
    NSLog(@"下载的进度:%f",prograss);
}
#pragma mark - 暂停
- (void)btnSuspendedAction:(UIButton*)sender{
    
    NSArray *marr = [[WKFMDBTools defaultFMDBToos]QueryDataWithTableName:kDBTableName section_videoid:self.model.section_videoid].mutableCopy;
    WKDownLoadModel *model = marr.firstObject;
    NSDictionary *dict = @{@"section_videoid":model.section_videoid,@"cgrade":model.cgrade,@"courseid":model.courseid,@"section_name":model.section_name};
    
    if ([self.delegate respondsToSelector:@selector(WKDownloadingCell:withSender:section_videoid:dict:)]) {
        
        [self.delegate WKDownloadingCell:self withSender:sender section_videoid:self.model.section_videoid dict:dict];
    }
    
}
- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    self.iconImageView.frame =CGRectMake(10, 15, 140, 120-30);
    self.titleLabel.frame =CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+10, 5, kDeviceScreenWidth-20-140-10, 42);
    self.prograssView.frame = CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+10, CGRectGetMaxY(self.titleLabel.frame)+15, kDeviceScreenWidth-20-140-10, 0.5);
    self.prograssLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImageView.frame)+10, CGRectGetMaxY(self.iconImageView.frame)-15, kDeviceScreenWidth-20-140-10, 21);
    self.btnSuspended.frame = CGRectMake(10, 15, 140, 120-30);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    
}

@end
